﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// http://blog.csdn.net/change_from_now/article/details/46503463#
/// </summary>
public class WrapContent : MonoBehaviour
{

    public class ItemInfo
    {
		public string spriteName="";
        public int ID = 0;
        public string Name = "";
        public ItemInfo(int id,string name,string iconName)
        {
            ID = id;
            Name = name;
			spriteName = iconName;
        }
    }

	public GameObject _wrap;
	UIWrapContent _wrapScript;
	public GameObject _box;
    List<ItemInfo> infos = new List<ItemInfo>();
	public int ItemNum = 21;
    // Use this for initialization  
    void Awake()
	{
		//获取脚本  
		_wrapScript = _wrap.GetComponent<UIWrapContent>();

		//绑定方法  
		_wrapScript.onInitializeItem = OnUpdateItem;

        infos = new List<ItemInfo>();
		//infos.Add(new ItemInfo(1, "AA"));
		//infos.Add(new ItemInfo(2, "BB"));
		//infos.Add(new ItemInfo(4, "DD"));
		//infos.Add(new ItemInfo(3, "CC"));
		//infos.Add(new ItemInfo(5, "EE"));
		//infos.Add(new ItemInfo(6, "FF"));
		//infos.Add(new ItemInfo(7, "GG"));
		//infos.Add(new ItemInfo(8, "HH"));
		//infos.Add(new ItemInfo(9, "I I"));
		//infos.Add(new ItemInfo(10, "GG"));

		for (int i = 0; i < ItemNum; i++)
		{
			infos.Add(new ItemInfo(i, Random.Range(1, 100).ToString(), "icon_"+Random.Range(1,18).ToString()));
		}
				
        InitList();
	}

	void InitList()
	{
		for (int i = 0; i < 5; i++)
		{
			GameObject go = Instantiate(_box) as GameObject;
			go.transform.parent = _wrap.transform;
			go.transform.localScale = Vector3.one;
			go.transform.localPosition = Vector3.zero;
		}
		_wrapScript.gameObject.GetComponent<UIGrid>().Reposition();
	}

	// Update is called once per frame  
	void Update()
	{
		//Test  
		if (Input.GetKeyDown(KeyCode.A))
		{
			_wrapScript.minIndex = 0;
			_wrapScript.maxIndex = 10;
			InitList();
			//启用脚本  
			_wrapScript.enabled = true;
		}
	}

	void OnUpdateItem(GameObject go, int index, int realIndex)
	{
        var absRealindex = Mathf.Abs(realIndex);// 取绝对值
        Debug.Log("index = " + index + " realIndex = " + absRealindex);  
		Testbox tb = go.GetComponent<Testbox>();//获取组建
		tb.SetNumber(absRealindex.ToString());//刷新UI数据
        if (infos[absRealindex] != null)
        {
            tb.SetData(infos[absRealindex]);
        }
		//_wrapScript.gameObject.GetComponent<UIGrid>().Reposition();
	}

}